package com.chen.rabbitMQ;


import com.chen.judge.JudgeService;
import com.chen.mapper.ContestCodeSubmitMapper;
import com.chen.mapper.ContestLeaderboardMapper;
import com.chen.mapper.ProblemSubmitMapper;
import com.chen.service.ContestLeaderboardService;
import com.chen.service.impl.ContestServiceImpl;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Component
@Slf4j
public class JudgeListenerQueue {

    @Autowired
    JudgeService judgeService;

    @Autowired
    ContestServiceImpl contestServiceImpl;

    @Autowired
    ProblemSubmitMapper problemSubmitMapper;

    @Autowired
    ContestCodeSubmitMapper contestCodeSubmitMapper;

    @Autowired
    ContestLeaderboardMapper contestLeaderboardMapper;

    @Autowired
    ContestLeaderboardService contestLeaderboardService;

    // 指定程序监听普通时的消息队列和确认机制
    @SneakyThrows
    @RabbitListener(queues = {"Direct.JudgeQueue"}, ackMode = "MANUAL")
    public void execJudge(String problemSubmitId, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        log.info("execJudge problemSubmitId = {}", problemSubmitId);
        Integer questionSubmitId = Integer.parseInt(problemSubmitId);
        try {
            judgeService.doJudge(questionSubmitId);
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, false);
        }
    }

    // 指定程序监听比赛时的消息队列和确认机制
//    @SneakyThrows
    @RabbitListener(queues = {"Direct.ContestQueue"}, ackMode = "MANUAL")
    public void contestExecJudge(String problemSubmitId, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {
        log.info("execJudge problemSubmitId = {}", problemSubmitId);
        Integer questionSubmitId = Integer.parseInt(problemSubmitId);
        try {
//            System.out.println(1);
            judgeService.doJudge(questionSubmitId);
//            System.out.println(2);
            try {
                contestServiceImpl.executeContestProblem(questionSubmitId);
                contestLeaderboardService.updateContestLeaderboard(questionSubmitId);
            } catch (Exception e) {
                log.error("调用比赛相关信息异常", e);
            }
//            System.out.println(3);
//            System.out.println("contestProblemId:" + contestProblemId);
//            contestCodeSubmitMapper.getByProblemSubmitId()
//            CompletableFuture.runAsync(() -> contestServiceImpl.executeContestProblem(questionSubmitId));
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, false);
        }
    }
}
